package message.event;

import lodsve.core.configuration.EnableLodsve;
import lodsve.core.event.EventPublisher;
import lodsve.core.event.annotations.AsyncEvent;
import lodsve.core.event.annotations.Events;
import lodsve.core.event.annotations.SyncEvent;
import lodsve.core.event.listener.EventListener;
import lodsve.core.event.module.BaseEvent;
import lodsve.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 日志事件监听器.
 *
 * @author sunhao(sunhao.java@gmail.com)
 * @version V1.0
 * @createTime 13-4-27 上午5:07
 */
@Component
@Events(
        async = {@AsyncEvent(name = "查询", event = QueryEvent.class)},
        sync = {@SyncEvent(name = "查看", event = ViewEvent.class)}
)
@EnableLodsve
public class LoggerListener implements EventListener {
    @Autowired
    private EventPublisher eventPublisher;

    @Override
    public void handleEvent(BaseEvent baseEvent) throws RuntimeException {
        String description = eventPublisher.evalName(baseEvent.getClass()) + ": " + baseEvent.getSource().toString();

        System.out.println("发生在: " + DateUtils.formatDate(baseEvent.getEventTime(), DateUtils.DEFAULT_PATTERN) + ", " + description);
    }
}
